
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import( '../views/login.vue'), //登入页面
    },

    {
        path: '/home',
        component: () => import( '../views/home.vue'), //后台主体框架
        redirect: '/system/welcome',
        children: [
            {
                path: '/system/welcome',
                name: 'Welcome',
                component: () => import( '../views/welcome.vue'), //系统欢迎页
            }, {
                path: '/system/emp',
                name: '员工管理',
                component: () => import( '../views/system/employee.vue'), //用户管理
            }, {
                path: '/system/roles',
                name: '角色管理',
                component: () => import( '../views/system/role.vue'), //角色管理
            }
            , {
                path: '/system/menu-category',
                name: '菜单管理',
                component: () => import( '../views/system/menu-category.vue'), //菜单管理
            },
            {
                path: '/system/sysnotice',
                name: '系统通知',
                component: () => import('../views/system/SysNotice.vue'),  //系统通知管理
            },
            {
                path: '/system/icon',
                name: '系统图标',
                component: () => import('../views/system/icon'),  //系统图标
            },
            {
                path: '/system/linetype',
                name: '线路类型管理',
                component: () => import('../views/system/LineType'),  //系统图标
            },
            // -----------
            {
                path: '/client/ClientManage',
                name: '客户管理',
                component: () => import('../views/business/client/ClientManage'),  //客户管理
            },
            {
                path: '/client/ClientAttention',
                name: '我的关注',
                component: () => import('../views/business/client/ClientAttention'),  //我的关注
            },
            {
                path: '/clientConcern/ClientConcern',
                name: '客户关怀管理',
                component: () => import('../views/business/clientConcern/ClientConcern'),  //客户关怀管理
            },
            {
                path: '/clientConcern/ClientConcernAttention',
                name: '我的关注',
                component: () => import('../views/business/clientConcern/ClientConcernAttention'),  //我的关注
            },
            {
                path: '/clientAccount/ClientAccountManage',
                name: '客户账户管理',
                component: () => import('../views/business/clientAccount/ClientAccountManage.vue'),  //客户账户管理
            },
            {
                path: '/user/UserManage',
                name: '用户管理',
                component: () => import('../views/business/user/UserManage'),  //用户管理
            },
            {
                path: '/dept/ClientDept',
                name: '客户部门',
                component: () => import('../views/business/dept/ClientDept.vue'),  //客户部门
            },
            {
                path: '/dept/EmpDept',
                name: '员工部门',
                component: () => import('../views/business/dept/EmpDept.vue'),  //员工部门
            },

            // 前台新添加的
            {
                path: '/comcentre/agent',
                name: '通信中心 / 坐席设置',
                component: () => import('../views/front/comcentre/Agent.vue'),  //坐席设置
            },
            {
                path: '/comcentre/calllog',
                name: '通信中心 / 通话记录',
                component: () => import('../views/front/comcentre/CallLog.vue'),  //通话记录
            },
            {
                path: '/comcentre/seattraffic',
                name: '通信中心 / 坐席话务统计',
                component: () => import('../views/front/comcentre/SeatTraffic.vue'),  //坐席话务统计
            },
            {
                path: '/comcentre/consumelog',
                name: '通信中心 / 日消费记录',
                component: () => import('../views/front/comcentre/ConsumeLog.vue'),  //日消费记录
            },
            {
                path: '/comcentre/rechargelog',
                name: '通信中心 / 充值记录',
                component: () => import('../views/front/comcentre/RechargeLog.vue'),  //充值记录
            },
            {
                path: '/tasking/taskgoverning',
                name: '任务分配 / 任务包管理',
                component: () => import('../views/front/tasking/TaskGoverning.vue'),  //任务包管理
            },
            {
                path: '/tasking/allocated',
                name: '任务分配 / 已分配资源',
                component: () => import('../views/front/tasking/Allocated.vue'),  //已分配资源
            },
            {
                path: '/tasking/undistributed',
                name: '任务分配 / 未分配资源',
                component: () => import('../views/front/tasking/Undistributed.vue'),  //未分配资源
            },
            {
                path: '/tasking/taskgoverning',
                name: '任务分配 / 任务包管理',
                component: () => import('../views/front/tasking/TaskGoverning.vue'),  //任务包管理
            },
            {
                path: '/myresource/resourcemanage',
                name: '我的资源 / 资源管理',
                component: () => import('../views/front/myresource/ResourceManage.vue'),  //资源管理
            },
            {
                path: '/client/frontclientmanage',
                name: '客户 / 客户管理',
                component: () => import('../views/front/client/ClientManage.vue'),  //客户管理
            },
            {
                path: '/client/highseasclient',
                name: '客户 / 公海客户',
                component: () => import('../views/front/client/HighSeasClient.vue'),  //公海客户
            },
            {
                path: '/client/clientshift',
                name: '客户 / 客户转移',
                component: () => import('../views/front/client/ClientShift.vue'),  //客户转移
            },
            {
                path: '/toolmodel/expiredwarning',
                name: '工具模块 / 过期预警',
                component: () => import('../views/front/toolmodel/ExpiredWarning.vue'),  //过期预警
            },
            {
                path: '/toolmodel/blacklist',
                name: '工具模块 / 黑名单',
                component: () => import('../views/front/toolmodel/Blacklist.vue'),  //黑名单
            },
            {
                path: '/toolmodel/recyclebin',
                name: '工具模块 / 回收站',
                component: () => import('../views/front/toolmodel/RecycleBin.vue'),  //回收站
            },
            {
                path: '/toolmodel/distribute',
                name: '工具模块 / 离职分配',
                component: () => import('../views/front/toolmodel/Distribute.vue'),  //数据字典
            }

            /**********************************业务模块的路由******************************/

        ]
    },
]

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
})

import store from '../store'//引入store

//白名单页面
const whiteList=[
    '/business/product/add-stocks',
    '/business/product/publish'
];

//路由导航守卫
router.beforeEach((to, from, next) => {

    document.title = to.name.substring(to.name.indexOf('/')+1) || '乐马优途AI学员管理服务系统';

    const token = LocalStorage.get(LOCAL_KEY_XINGUAN_ACCESS_TOKEN);
    if (to.path === '/login') {
        if (!token) {
            return next();
        } else {
            return next({path: '/home'})
        }
    }

    if (to.path === '/error/401') {
        return next();
    }

    if (to.path === '/home') {
        return next();
    }
    if (to.path === '/system/welcome') {
        return next();
    }

    if (!token) {
        return next('/login');
    } else {
        //判断是否有访问该路径的权限
        const urls = store.state.userInfo.url;
        //如果是管理员
        if (store.state.userInfo.isAdmin) {
            return next();
        } else {
            if (urls.indexOf(to.path) > -1|| whiteList.indexOf(to.path)>-1) {
                //则包含该元素
                window.sessionStorage.setItem("activePath", to.path);
                return next();
            } else {
                return next("/error/401");
            }
        }
    }
})

export default router
